package com.yuri.news.jpa;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.yuri.news.entity.Classify;

/**
 * 分类jpa
 * @author Yuri
 */
public interface ClassifyRepository extends JpaRepository<Classify, Long>, JpaSpecificationExecutor<Classify> {

	/**
	 * 根据分类id查询该分类下的分类
	 * @param id
	 * @return
	 */
	@Query(value = "SELECT * FROM t_classify  cy2 INNER JOIN t_classify cy1 ON cy2.up_classify_id = cy1.id and cy1.id = ?1", nativeQuery = true)
	List<Classify> findDowClassById(Long id);

	/**
	 * 根据分类id查询该分类下的分类
	 * @param id	
	 * @param page	分页信息
	 * @return
	 */
	@Query(value = "SELECT * FROM t_classify  cy2 INNER JOIN t_classify cy1 ON cy2.up_classify_id = cy1.id and cy1.id = ?1",
			countQuery = "SELECT count(*) FROM t_classify  cy2 INNER JOIN t_classify cy1 ON cy2.up_classify_id = cy1.id and cy1.id = ?1",
			nativeQuery = true)
	Page<Classify> findDowClassById(Long id, Pageable page);
	
	/**
	 * 查询一级分类
	 * @return
	 */
	List<Classify> findByUpClassifyIsNull();
	
	/**
	 * 查询一级分类(分页)
	 * @param page	分页信息
	 * @return
	 */
	Page<Classify> findByUpClassifyIsNull(Pageable page);
}
